package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.CarCharge;
import com.group.Enity.House;
import com.group.Service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.crypto.Data;
import java.util.List;

@Controller
@RequestMapping("house")
public class HouseController {


    @Autowired
    private HouseService houseService;

    @ResponseBody
    @RequestMapping("houseAll")
    public JSONObject houseAll(Integer page,Integer limit,String numbers)
    {
        PageHelper.startPage(page,limit);
        List<House> list = houseService.getHouses(numbers);
        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json;
    }

    @ResponseBody
    @RequestMapping("add")
    public String add(@RequestBody JSONObject jsonObject){

        //System.out.println(jsonObject);
        Double area = jsonObject.getDouble("area");
        String numbers = jsonObject.getStr("numbers");
        int buildingId = jsonObject.getInt("buildingId");
        String date = jsonObject.getStr("intoDate");
        String remarks = jsonObject.getStr("remarks");

        House house = new House();
        house.setBuilding_id(buildingId);
        house.setArea(area);
        house.setIntoDate(date);
        if (remarks != null)
            house.setRemarks(remarks);
        house.setNumbers(numbers);

        houseService.add(house);
        //houseService.addByInfor(area,numbers,buildingId,date,remarks);

        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        json.accumulate("msg","房屋添加成功");
        return json.toString();
    }


    @ResponseBody
    @RequestMapping("queryAll")
    public List<House> update() {
        List<House> list = houseService.getHouses(null);
        return list;
    }

    @ResponseBody
    @RequestMapping("update")
    public String update(@RequestBody JSONObject jsonObject){

        Double area = jsonObject.getDouble("area");
        String numbers = jsonObject.getStr("numbers");
        int buildingId = jsonObject.getInt("buildingId");
        String date = jsonObject.getStr("intoDate");
        String remarks = jsonObject.getStr("remarks");
        int id = jsonObject.getInt("id");

        House house = new House();
        house.setId(id);
        house.setBuilding_id(buildingId);
        house.setArea(area);
        house.setIntoDate(date);
        if (remarks != null)
            house.setRemarks(remarks);
        house.setNumbers(numbers);

        houseService.update(house);

        //System.out.println(jsonObject);
        return "{\"code\":200}";
    }

    @ResponseBody
    @RequestMapping("deleteByIds")
    public String deleteByIds(String ids){
        //System.out.println(ids);
        String[] lst = ids.split(",");
        for (int index = 0;index < lst.length;index++)
        {
            int id = Integer.valueOf(lst[index]);
            houseService.delete(id);
        }

        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json.toString();
    }

}
